#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include "../../../vcd-common-api/vcd-util.h"
#include "../../../vcd-common-api/vcd-common.h"
#include "../../../vcd-common-api/vcd-log.h"
#include "vcd-sys-info.h"

static GtkWidget *get_sys_info_fix(void);

static char* get_cpus(void);
static char* get_mem(void);
static char* get_disk(void);
static char* get_boot_time(void);

GtkWidget *get_sys_info_grid(void)
{
    GtkWidget* grid_sys_info;
    GtkWidget* fix_sys_info;

    grid_sys_info = gtk_grid_new();
    fix_sys_info = get_sys_info_fix();

    gtk_grid_attach((GtkGrid *)grid_sys_info, fix_sys_info, 0, 0, 1, 1);

    return grid_sys_info;
}

static GtkWidget *get_sys_info_fix(void)
{
    char *mac  = NULL;
    char *cpu  = NULL;
    char *mem  = NULL;
    char *disk = NULL;
    char *time = NULL;

    GtkWidget *fix_sys_info = gtk_fixed_new();

    GtkWidget *grid_label_cpu;
    GtkWidget *grid_label_mem;
    GtkWidget *grid_label_disk;
    GtkWidget *grid_label_mac;
    GtkWidget *grid_label_time;

    GtkWidget *label_cpu;
    GtkWidget *label_mem;
    GtkWidget *label_disk;
    GtkWidget *label_mac;
    GtkWidget *label_time;

    label_cpu  = gtk_label_new(NULL);
    label_mem  = gtk_label_new(NULL);
    label_disk = gtk_label_new(NULL);
    label_mac  = gtk_label_new(NULL);
    label_time = gtk_label_new(NULL);
    gtk_widget_set_size_request(label_cpu,  70, 70);
    gtk_widget_set_size_request(label_mem,  70, 70);
    gtk_widget_set_size_request(label_disk, 70, 70);
    gtk_widget_set_size_request(label_mac,  70, 70);
    gtk_widget_set_size_request(label_time, 70, 70);

    cpu  = get_cpus();
    mem  = get_mem();
    disk = get_disk();
    mac  = get_mac();
    time = get_boot_time();

    grid_label_cpu  = gtk_label_new(cpu);
    grid_label_mem  = gtk_label_new(mem);
    grid_label_disk = gtk_label_new(disk);
    grid_label_mac  = gtk_label_new(mac);
    grid_label_time = gtk_label_new(time);

    SAFE_FREE(cpu);
    SAFE_FREE(mem);
    SAFE_FREE(disk);
    SAFE_FREE(mac);
    SAFE_FREE(time);

    gtk_fixed_put((GtkFixed *)fix_sys_info, label_cpu,  90,  65);
    gtk_fixed_put((GtkFixed *)fix_sys_info, label_mem,  260, 65);
    gtk_fixed_put((GtkFixed *)fix_sys_info, label_disk, 430, 65);
    gtk_fixed_put((GtkFixed *)fix_sys_info, label_mac,  90,  225);
    gtk_fixed_put((GtkFixed *)fix_sys_info, label_time, 260, 225);

    gtk_fixed_put((GtkFixed *)fix_sys_info, grid_label_cpu,  98,  145);
    gtk_fixed_put((GtkFixed *)fix_sys_info, grid_label_mem,  264, 145);
    gtk_fixed_put((GtkFixed *)fix_sys_info, grid_label_disk, 434, 145);
    gtk_fixed_put((GtkFixed *)fix_sys_info, grid_label_mac,  40,  305);
    gtk_fixed_put((GtkFixed *)fix_sys_info, grid_label_time, 219, 305);

    set_img(label_cpu,  "label", "cpu.png");
    set_img(label_mem,  "label", "mem.png");
    set_img(label_disk, "label", "disk.png");
    set_img(label_mac,  "label", "mac.png");
    set_img(label_time, "label", "time.png");
    return fix_sys_info;
}

static char *get_cpus(void)
{
    char *string_cpu = (char *)malloc(20 * sizeof(char));
    if (NULL == string_cpu) {
        M_DEBUG("get mem error");
        return NULL;
    }

#ifndef __linux__
    strcpy(string_cpu, " ");
    return string_cpu;
#endif

    strcpy(string_cpu, "\nCPU:");

    size_t si = 0;
    char* s = exe_command("lscpu|grep \"CPU(s)\"|awk '{print $2}'|head -1", &si);
    if(s == NULL){
        return NULL;
    }
    char tmp_cpu[14] = {0};
    snprintf(tmp_cpu, 14, "%s核", s);
    strcat(string_cpu, tmp_cpu);
    SAFE_FREE(s);

    return string_cpu;
}

static char *get_mem(void)
{
    char *string_mem = (char *)malloc(20 * sizeof(char));
    if (NULL == string_mem) {
        M_DEBUG("get mem error");
        return NULL;
    }

#ifndef __linux__
    strcpy(string_mem, " ");
    return string_mem;
#endif
    strcpy(string_mem, "\n内存:");

    char mem[16] = {0};
    FILE * pf = popen("free -h|awk '{print $2}'", "r");
    if (!fgets(mem, sizeof(mem), pf))
    {
        M_DEBUG("get_mem error");
        pclose(pf);
        return string_mem;
    }
    if (!fgets(mem, sizeof(mem), pf))
    {
        M_DEBUG("get_mem error");
        pclose(pf);
        return string_mem;
    }
    strcat(string_mem, mem);
    string_mem[strlen(string_mem) - 1] = 0;
    pclose(pf);
    return string_mem;
}

static char *get_disk(void)
{
    char *string_disk = (char *)malloc(20 * sizeof(char));
    if (NULL == string_disk) {
        M_DEBUG("get disk error");
        return NULL;
    }

#ifndef __linux__
    strcpy(string_disk, " ");
    return string_disk;
#endif
    strcpy(string_disk, "\n硬盘:");

    char buff[10] = {0};
    FILE *pf = popen("lsblk|grep 'disk'|awk '{print $4}'", "r");
    if (!fgets(buff, sizeof(buff), pf))
    {
        M_DEBUG("get disk error");
        return string_disk;
    }
    strcat(string_disk, buff);
    pclose(pf);
    return string_disk;
}

char *get_mac(void)
{
    char *string_mac = (char *)malloc(50 * sizeof(char));
    if (NULL == string_mac) {
        M_DEBUG("get mac error");
        return NULL;
    }


#ifndef __linux__
    strcpy(string_mac, "");
    return string_mac;
#endif
    strcpy(string_mac, "\nMAC地址:");

    char mac[40] = {0};
    FILE *pf = popen("ifconfig|grep ether|awk '{print $2}'", "r");
    if (!fgets(mac, 40, pf))
    {
        M_DEBUG("get_mac error");
        pclose(pf);
        return string_mac;
    }
    strcat(string_mac, mac);
    string_mac[strlen(string_mac) - 1] = 0;
    pclose(pf);
    return string_mac;
}

char *get_mac_(void)
{
    char *stringmac = (char *)malloc(50 * sizeof(char));
    if (NULL == stringmac) {
        M_DEBUG("get mac error");
        return NULL;
    }
#ifndef __linux__
    return stringmac;
#endif

    FILE *pf;
    pf = popen("ifconfig|grep ether|awk '{print $2}'", "r");
    if (!fgets(stringmac, 40, pf))
    {
        M_DEBUG("get_mac error");
        pclose(pf);
        return stringmac;
    }
    stringmac[strlen(stringmac) - 1] = 0;
    pclose(pf);
    return stringmac;
}

static char *get_boot_time(void)
{
    char *string_time = (char *)malloc(50 * sizeof(char));
    if (NULL == string_time) {
        M_DEBUG("get time error");
        return NULL;
    }
#ifndef __linux__
    strcpy(string_time, " ");
    return string_time;
#endif

    char timeTmp[25] = {0};
    int i = 0;
    int j = 0;
    FILE *pf;
    char buff[100] = {0};
    pf = popen("who -b", "r");
    if (!fgets(buff, 100, pf))
    {
        M_DEBUG("get_time error");
        pclose(pf);
        return string_time;
    }
    for (i = 0; i < strlen(buff) - 1; i++)
    {
        if (buff[i] == ' ' && buff[i + 1] == '2')
        {
            i++;
            for (j = 0; i < strlen(buff) - 1; i++, j++)
            {
                timeTmp[j] = buff[i];
            }
            timeTmp[j] = '\0';
            break;
        }
    }
    strcpy(string_time, "\n启动时间:");
    strcat(string_time, timeTmp);
    pclose(pf);
    return string_time;
}

char *get_pve_info(const char* cmd, int length)
{
    char string[50] = {0};
    FILE *pf;
    pf = popen(cmd, "r");
    if (!pf) {
        return NULL;
    }
    if (!fgets(string, length, pf))
    {
        pclose(pf);
        return NULL;
    }
    pclose(pf);
    string[strlen(string) - 1] = 0;
    return strdup(string);
}